package com.lsh.controller;

import com.lsh.dao.UserDao;
import com.lsh.dao.UserHeadPortraitDao;
import com.lsh.dao.UserTokenDao;
import com.lsh.domain.Article;
import com.lsh.domain.User;
import com.lsh.domain.UserHeadPortrait;
import com.lsh.service.ArticleService;
import com.lsh.service.IndexService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.ibatis.annotations.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 文章访问模块
 */
@Controller
public class ArticleController {

    @Autowired
    ArticleService articleService;
    @Autowired
    IndexService indexService;

    /**
     * 展示文章api
     * @param type
     * @param mv
     * @return
     */
    @RequestMapping("/api/article")
    public ModelAndView showArticle(@RequestParam(name = "type", required = false) String type, ModelAndView mv){
        if("home".equals(type)){

        }else{
            List articles = articleService.findArticleList();
            JSONObject jo = new JSONObject();
            jo.put("articles", articles);
            mv.addObject("data", jo.toString());
        }
        mv.setViewName("/JSON");
        return mv;
    }

    /**
     * 进入markdown编辑页面
     * @param mv
     * @return
     */
    @RequestMapping(value = {"/user/markdown/{artId}", "/user/markdown"})
    public ModelAndView openMarkdown(@CookieValue("TOKEN") String token, @PathVariable(value = "artId", required = false) String artId, ModelAndView mv){
        int userId = Integer.parseInt(indexService.findByUserId(token));
        Map articleMap = null;
        if(artId != null){
            articleMap = articleService.findOpenMarkdownList(userId, artId); //编辑旧文章
        }else{
            articleMap = articleService.findOpenMarkdownList(userId, null); //写新文章
        }
        mv.addObject("articleMap", articleMap);
        mv.setViewName("/user/article/markdown");
        return mv;
    }

    /**
     * 浏览文章页面
     * @param username
     * @param artId
     * @param mv
     * @return
     */
    @RequestMapping("/{username}/article/details/{artId}")
    public ModelAndView browseArticle(@PathVariable("username") String username, @PathVariable("artId") String artId,
                                      @CookieValue("TOKEN") String token, ModelAndView mv){
        String nickname = indexService.findNickname(token);
        Map article = articleService.findArticleDetail(username, artId);
        mv.addObject("nickname", nickname);
        mv.addObject("article", article);
        mv.setViewName("/user/article/article_browse");
        return mv;

    }

    /**
     * 发表文章 (修改)
     * @param token
     * @param article
     * @param contentMd
     * @param contentHtml
     * @param mv
     * @return
     */
    @PostMapping(value = "/user/article/publish")
    public ModelAndView publishArticle(Article article, ModelAndView mv, @CookieValue("TOKEN") String token,
                                       @RequestParam("editormd-markdown-doc") String contentMd,
                                       @RequestParam("editormd-html-code") String contentHtml){
        article.setContentMd(contentMd);
        article.setContentHtml(contentHtml);
        System.out.println(article.getTitle() + "," + article.getContentHtml());
        String result = articleService.publishArticle(article, token);
        JSONObject jo = new JSONObject();
        jo.put("resultMsg", result);
        mv.addObject("data", jo.toString());
        mv.setViewName("/JSON");
        return mv;
    }

    /**
     * 保存文章至草稿
     * @param token
     * @param article
     * @param contentMd
     * @param contentHtml
     * @param mv
     * @return
     */
    @PostMapping(value = "/user/article/save")
    public ModelAndView saveArticle(Article article, ModelAndView mv, @CookieValue("TOKEN") String token,
                                       @RequestParam("editormd-markdown-doc") String contentMd,
                                       @RequestParam("editormd-html-code") String contentHtml){
        article.setContentMd(contentMd);
        article.setContentHtml(contentHtml);
        Map<String, Object> map = articleService.saveArticle(article, token);
        JSONObject jo = new JSONObject();
        jo.put("result", map);
        mv.addObject("data", jo.get("result"));
        mv.setViewName("/JSON");
        return mv;
    }
}
